package com.yeyks.service.impl;

import com.yeyks.constant.MessageTemplate;
import com.yeyks.dal.dao.MessageNoticeTemplateMapper;
import com.yeyks.dal.domain.MessageNoticeTemplate;
import com.yeyks.service.MessageNoticeTemplateService;
import com.yeyks.util.FormatUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fuchangshuai
 * @date 2019/4/23 0023
 */
@Service
@Slf4j
public class MessageNoticeTemplateServiceImpl implements MessageNoticeTemplateService {

    @Autowired
    private MessageNoticeTemplateMapper messageNoticeMapper;

    /**
     * 通过模板id获取模板对象
     *
     * @param template
     * @return
     */
    @Override
    public MessageNoticeTemplate getTemplate(String template) {
        return messageNoticeMapper.selectTemplateValue(template);
    }

    /**
     * 通过订单模板获取操作后的值(没有模板返回null)
     *
     * @param template
     * @param hotelName
     * @param checkIn
     * @param checkOut
     * @param count
     * @return
     */
    @Override
    public Map<String, String> orderAfterHandleValue(String template, String hotelName, String checkIn, String checkOut, Integer count) {
        Map<String, String> map = new HashMap<>();
        try {
            MessageNoticeTemplate noticeTemplate = messageNoticeMapper.selectTemplateValue(template);
            if (noticeTemplate == null) {
                return map;
            }
            map.put("title", noticeTemplate.getTitle());
            map.put("iconUrl", noticeTemplate.getIconUrl());
            String content = noticeTemplate.getContent();
            String value = getValue(content, checkIn, checkOut, count, hotelName);
            map.put("content", value);
            return map;
        } catch (Exception e) {
            log.error("通过订单模板获取操作后的值{}", e.getLocalizedMessage());
            return map;
        }
    }

    /**
     * 获取模板值通用
     *
     * @param params
     * @param template
     * @return
     */
    @Override
    public Map<String, String> commonValue(Map<String, Object> params, String template) {
        Map<String, String> map = new HashMap<>();
        try {
            MessageNoticeTemplate noticeTemplate = messageNoticeMapper.selectTemplateValue(template);
            if (noticeTemplate == null) {
                return map;
            }
            map.put("title", noticeTemplate.getTitle());
            map.put("iconUrl", noticeTemplate.getIconUrl());
            map.put("content", FormatUtils.formatTemplate(noticeTemplate.getContent(), params));
            return map;
        } catch (Exception e) {
            log.error("获取模板值通用{}", e.getLocalizedMessage());
            return map;
        }
    }

    private String getValue(String content, String checkIn, String checkOut, Integer count, String hotelName) {
        Map<String, Object> map = new HashMap<>();
        map.put("checkIn", checkIn);
        map.put("checkOut", checkOut);
        map.put("count", count);
        map.put("hotelName", hotelName);
        return FormatUtils.formatTemplate(content, map);
    }
}
